package com.starblink.brand.ui

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.skydoves.balloon.balloon
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.base.activity.BaseTVMActivity
import com.starblink.android.basic.ext.bindProductFeeds
import com.starblink.android.basic.ext.inflater
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.basic.route.RoutePage
import com.starblink.brand.adapter.BrandCollectionHolder
import com.starblink.brand.adapter.BrandMoreCardHolder
import com.starblink.brand.adapter.BrandMoreEndHolder
import com.starblink.brand.adapter.BrandMoreTitleHolder
import com.starblink.brand.bean.BrandMoreEndBean
import com.starblink.brand.bean.BrandMoreTitleBean
import com.starblink.brand.databinding.*
import com.starblink.brand.model.BrandCollectionVM
import com.starblink.brand.ui.part.PartSort
import com.starblink.brand.widget.BrandFilterDialog
import com.starblink.brand.widget.BrandProductBalloonFactory
import com.starblink.search.result.ui.cell.GoodsEmptyCell

@Deprecated(message = "已废弃 1.15.2")
@Route(path = RoutePage.Brand.BRAND_COLLECTION)
class BrandCollectionActivity : BaseTVMActivity<ActivityBrandCollectionBinding, BrandCollectionVM>() {
    @JvmField
    @Autowired(name = RoutePage.Brand.ARG_brandName)
    var brandName: String? = null

    @JvmField
    @Autowired(name = RoutePage.Brand.ARG_brandCollectionId)
    var brandCollectionId: String? = null

    @JvmField
    @Autowired(name = RoutePage.Brand.ARG_productId)
    var productId:String?=null

    private val balloon  by  balloon<BrandProductBalloonFactory>()
    private var partSort:PartSort?=null
    private var ifHavefilterCategory=true//是否有一级分类
    private val adapter: MutableAdapter by lazyOnNone {
        MutableAdapter(viewModel.brandCollectionObserveData).apply {
            addVhDelegate {
                //商品
                BrandCollectionHolder(this@BrandCollectionActivity, it,brandName){position ->
                    if(viewModel.hasNext&&position==viewModel.brandCollectionObserveData.size-4){
                        //自动加载更多
                        viewModel.reqBrandDetailData(brandCollectionId,productId,initData =false)
                    }
                }
            }
            addVhDelegate {
                //无商品
                GoodsEmptyCell(it)
            }
            addVhDelegate {
                //推荐品牌title
                BrandMoreTitleHolder(ItemBrandMoreTitleBinding.inflate(it.inflater,it,false))
            }
            addVhDelegate {
                //推荐品牌card
                BrandMoreCardHolder(ItemBrandMoreCardBinding.inflate(it.inflater,it,false))
            }
            addVhDelegate {
                //end
                BrandMoreEndHolder(ItemBrandTheEndBinding.inflate(it.inflater,it,false))
            }

        }
    }

    override fun initViewBinding(): ActivityBrandCollectionBinding {
        return ActivityBrandCollectionBinding.inflate(layoutInflater)
    }

    override fun initData() {
        super.initData()
        viewBinding.tvTitle.text="$brandName"
        viewBinding.smartRefreshLayout.setEnableRefresh(true)
        viewBinding.smartRefreshLayout.setOnRefreshListener {
            viewModel.reqBrandDetailData(brandCollectionId,productId)
        }
        viewBinding.smartRefreshLayout.setEnableLoadMore(true)
        viewBinding.smartRefreshLayout.setOnLoadMoreListener {
//            if(viewModel.hasNext){
//                viewModel.reqBrandDetailData(brandCollectionId,productId,initData =false)
//            }else{
//                viewBinding.smartRefreshLayout.finishLoadMore(500)
//            }

            //已经有上拉自动加载 这里不再触发接口请求
            viewBinding.smartRefreshLayout.finishLoadMore(500)
        }

//        val linearLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
//        viewBinding.recyclerView.addItemDecoration(
//            SimpleGapItemDecor(
//                withEdge = true,
//                horGap = 10.dp2px(),
//                edgeGap = 10.dp2px()
//            )
//        )
//        viewBinding.recyclerView.layoutManager = linearLayoutManager
//        viewBinding.recyclerView.adapter = adapter

        viewBinding.recyclerView.bindProductFeeds(adapter, horGap = 11, verGap = 0)


        viewBinding.btnSort.setOnClickListener {
            //条件排序
            showSortPopup(it)
        }
        viewBinding.btnFilter.setOnClickListener{
            //筛选搜索
            showFilterDialog()
        }
    }

    override fun initView() {
        super.initView()
        pageExposure(SpmPageDef.BrandDetailPage, params = mapOf("brandName" to (brandName?:"")))



        viewModel.reqBrandDetailData(brandCollectionId,productId,ifShowLoading = true)
        viewModel.getFilterCategoryData(brandCollectionId?:"")
    }

    override fun initObservable() {
        super.initObservable()
        viewModel.brandCollectionInfo.observe(this){
            //品牌logo
//            viewBinding.ivBrandLogo.load(it.brandLogo, it.brandName)
//            viewBinding.tvBrandName.text=it.brandName
//            viewBinding.tvBrandProducts.text = "${if(it.productNum>1000) "1000+" else it.productNum} Products"
            val discountStr="UP TO ${it.lowestDiscount.toFloat().toInt()}% OFF"
            viewBinding.tvDiscountTip.visibleOrGone(true){
                text="$discountStr  -  $discountStr  -  $discountStr"
            }
        }
        viewModel.productList.observe(this) {
            if (it.isNotEmpty()||viewModel.brandCollectionInfo.value!=null) {
                viewBinding.ivLoading.visibleOrGone(false) {}
                viewBinding.coordinator.visibleOrGone(true) {}
                viewBinding.smartRefreshLayout.visibleOrGone(true) {}

                if (viewModel.pageNo == 1) {
                    //首页 滑动顶部
                    viewBinding.recyclerView.scrollToPosition(0)
                }

                val indexTag = adapter.itemCount
                viewModel.brandCollectionObserveData.addAll(it)
                if (indexTag == 0) {
                    adapter.refreshAll()
                } else {
                    adapter.notifyItemRangeInserted(indexTag, it.size)
                }
            }
            if(viewBinding.smartRefreshLayout.isRefreshing){
                viewBinding.smartRefreshLayout.finishRefresh()
            }
            if (viewBinding.smartRefreshLayout.isLoading) {
                viewBinding.smartRefreshLayout.finishLoadMore()
            }
        }
        viewModel.brandRandomList.observe(this){
            if (it.isNotEmpty()) {
                viewBinding.ivLoading.visibleOrGone(false) {}
                viewBinding.coordinator.visibleOrGone(true) {}
                viewBinding.smartRefreshLayout.visibleOrGone(true) {}

                val indexTag = adapter.itemCount
                viewModel.brandCollectionObserveData.add(BrandMoreTitleBean())
                viewModel.brandCollectionObserveData.addAll(it)
                viewModel.brandCollectionObserveData.add(BrandMoreEndBean())
                adapter.notifyItemRangeInserted(indexTag, it.size+2)
            }
            //无数据显示empty
            viewBinding.empty.visibleOrGone(viewModel.brandCollectionObserveData.isEmpty()&&viewModel.brandCollectionInfo.value==null) {
                viewBinding.ivLoading.visibleOrGone(false) {}
                viewBinding.coordinator.visibleOrGone(false) {}
                viewBinding.smartRefreshLayout.visibleOrGone(false) {}
            }
        }
        viewModel.filterCategoryListResult.observe(this) {
            //是否有一级分类
            ifHavefilterCategory=viewModel.filterCategoryList.isNotEmpty()
        }
    }

    override fun initLoad() {
        super.initLoad()
    }

    private fun showSortPopup(view: View){
        if(partSort==null) {
            partSort = PartSort(this,balloon){
                //设置排序条件
                viewModel.sortType=it.sort
                viewModel.reqBrandDetailData(brandCollectionId,productId,ifShowLoading = true)
            }.apply {
                handleVM()
            }
        }
        balloon.showAlignBottom(view)
    }
    private fun showFilterDialog(){
        BrandFilterDialog(
            mActivity,
            brandCollectionId,
            viewModel.lowestPrice,
            viewModel.highestPrice,
            viewModel.frontCategoryIds,
            ifHavefilterCategory
        ) {lowestPrice,highestPrice,frontCategoryIds,ifShowLoading ->
            viewModel.lowestPrice=lowestPrice
            viewModel.highestPrice=highestPrice
            viewModel.frontCategoryIds=frontCategoryIds
            viewModel.reqBrandDetailData(brandCollectionId,productId,ifShowLoading = ifShowLoading)
        }.show(supportFragmentManager, "filter")
    }
}